package com.xasz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xasz.entity.*;
import com.xasz.mapper.*;
import com.xasz.service.IResultService;
import com.xasz.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cai
 * @since 2022-07-28
 */
@Service
public class ResultServiceImpl extends ServiceImpl<ResultMapper, Result> implements IResultService {
    @Autowired
    CandidateMapper candidateMapper;
    @Autowired
    JuryMapper juryMapper;
    @Autowired
    ResultMapper resultMapper;
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    WeightMapper weightMapper;

    @Override
    public void creatResult(Integer activityId, Integer Uid) {
        ////拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",activityId)).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限创建该活动记录表");
        }
        QueryWrapper<Jury> wrapperJ=new QueryWrapper<Jury>()
                .eq("activity_id",activityId);
        QueryWrapper<Candidate> wrapperC=new QueryWrapper<Candidate>()
                .eq("activity_id",activityId);
        List<Jury> jurys=juryMapper.selectList(wrapperJ);
        List<Candidate> candidates=candidateMapper.selectList(wrapperC);
        if(jurys.size()==0){
            throw  new NotNxistException("未增加评分人");
        }
        if(candidates.size()==0){
            throw  new NotNxistException("未增加候选人");
        }
        for (Jury jury:jurys){
            for (Candidate candidate:candidates){
                int juryId= jury.getId();
                int candidateId=candidate.getId();
                //查询是否重复
                QueryWrapper<Result> wrapper=new QueryWrapper<Result>()
                        .eq("activity_id",activityId)
                        .eq("jury_id",juryId)
                        .eq("candidate_id",candidateId);
                Result results= resultMapper.selectOne(wrapper);
                if (results!=null){
                    continue;
                }
                //不重复则增加评分记录
                Result result=new Result();
                result.setActivityId(activityId);
                result.setJuryId(juryId);
                result.setCandidateId(candidateId);
                if (resultMapper.insert(result)!=1){
                    throw new CreatException("生成评分记录时出现错误"+result);
                }
            }
        }
    }
    @Override
    public void detectResultByJury(Integer activityId, Integer juryId,  Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",activityId)).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限创建该活动记录表");
        }
        QueryWrapper<Result> wrapper=new QueryWrapper<Result>()
                .eq("activity_id",activityId)
                .eq("jury_id",juryId);
        resultMapper.delete(wrapper);
    }
    @Override
    public void detectResultByCandidate(Integer activityId, Integer Id, Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",activityId)).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限创建该活动记录表");
        }
        QueryWrapper<Result> wrapper=new QueryWrapper<Result>()
                .eq("activity_id",activityId)
                .eq("candidate_id",Id);
        resultMapper.delete(wrapper);
    }

    private Double getaDouble(Double scoreA, Double scoreB, Double scoreC, Double score, Double weightA, Double weightB, Double weightC) {
        if(scoreA !=null){
            //分数、是否合理
            if (scoreA <=0 || scoreA >100){
                throw new ScoreException("分数需要在0——100间！");
            }
            score+= scoreA * (weightA==null?1:weightA);
        }
        if(scoreB !=null){
            //分数、是否合理
            if (scoreB <=0 || scoreB >100){
                throw new ScoreException("分数需要在0——100间！");
            }
            score+= scoreB *(weightB==null?1:weightB);
        }
        if(scoreC !=null){
            //分数、是否合理
            if (scoreC <=0 || scoreC >100){
                throw new ScoreException("分数需要在0——100间！");
            }
            score+= scoreC *(weightC==null?1:weightC);
        }
        return score;
    }
    @Override
    public void upDataResult(Result result, Integer juryId, Integer Uid) {
        Integer activityId=result.getActivityId();
        //参与权限判断
        if (juryMapper.selectOne(new QueryWrapper<Jury>().eq("activity_id",activityId).eq("jury_id",Uid))==null){
            throw new DeleteException("你没有权限查看该活动维度信息");
        }
        Integer candidateId=result.getCandidateId();
        Double ScoreA=result.getScoreA();
        Double ScoreB=result.getScoreB();
        Double ScoreC=result.getScoreC();
        Double ScoreD=result.getScoreD();
        Double ScoreE=result.getScoreE();
        Double ScoreF=result.getScoreF();
        //判断活动是否过期
        Activity activity=activityMapper.selectById(activityId);
        if (LocalDateTime.now().isAfter(activity.getEndTime())){
            throw new TimeException("活动已结束");
        }
        if (LocalDateTime.now().isBefore(activity.getStartTime())){
            throw new TimeException("活动未开始");
        }
        //活动、评委、候选人存在
        //不选择 jury candidate联合查询 数据量大时太慢 X
        //jury candidate单表查询 数据组合
        QueryWrapper<Jury> wrapperJ=new QueryWrapper<Jury>()
                .eq("activity_id",activityId)
                .eq("id",juryId);
        Jury jury=juryMapper.selectOne(wrapperJ);
        if(jury==null){
            throw new NotNxistException("你无权进行评分");
        }
        QueryWrapper<Candidate> wrapperC=new QueryWrapper<Candidate>()
                .eq("activity_id",activityId)
                .eq("id",candidateId);
        if(candidateMapper.selectList(wrapperC).size()==0){
            throw new NotNxistException("候选人不存在");
        }
        //获取各项权重
        QueryWrapper<Weight> wrapperW=new QueryWrapper<Weight>()
                .eq("activity_id",activityId);
        List<Weight> list=weightMapper.selectList(wrapperW);
        Double WeightA=list.get(0).getWeight();
        Double WeightB=list.get(1).getWeight();
        Double WeightC=list.get(2).getWeight();
        Double WeightD=list.get(3).getWeight();
        Double WeightE=list.get(4).getWeight();
        Double WeightF=list.get(5).getWeight();
        //开始计算分数
        Double score=0.0;
        score = getaDouble(ScoreA, ScoreB, ScoreC, score, WeightA, WeightB, WeightC);
        score = getaDouble(ScoreD, ScoreE, ScoreF, score, WeightD, WeightE, WeightF);
        //修改分数
        UpdateWrapper<Result> updateWrapper=new UpdateWrapper<Result>()
                .eq("activity_id",activityId)
                .eq("jury_id",juryId)
                .eq("candidate_id",candidateId);
        //权重为空时默认为1
        result.setScore(score*(jury.getWeight()==null?1:jury.getWeight()));
        if (resultMapper.update(result,updateWrapper)!=1){
            throw new UpdateException("评分时出现错误");
        }
    }
}
